package cn.edu.jxau.test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;

public class Main {

	public static void main(String[] args) {
		ListNode node = new ListNode(1);
		node.append(2).append(3).append(4).append(5).append(6);
		System.out.println(FindKthToTail(node, 0).val);
	}

	public static ListNode FindKthToTail(ListNode node, int k) {

		if (k == 0) {
			return null;
		}
		ListNode fast = node;
		int i = 0;
		for (; i < k && fast != null; i++) {
			fast = fast.next;
		}
		if (i < k && fast == null) {
			return null;
		}
		ListNode slow = node;
		while (fast != null) {
			fast = fast.next;
			slow = slow.next;
		}
		return slow;
	}
}

class ListNode {
	public int val;
	public ListNode next;

	ListNode(int val) {
		this.val = val;
	}

	public ListNode append(int val) {

		ListNode node = new ListNode(val);
		node.next = this.next;
		this.next = node;
		return node;
	}
}
